/*
 * Surface.cpp
 *
 *  Created on: 15/07/2008
 *      Author: Samuk
 */

#include "CSurface.h"

#include <string>

CSurface::CSurface() {
	//
}

CSurface::~CSurface() {
	//
}

SDL_Surface* CSurface::OnLoad(std::string File) {
	if ((*GetSurfaces())[File] != NULL) {
		return (*GetSurfaces())[File];
	}

	SDL_Surface* SurfTemp = NULL;
	SDL_Surface* SurfRetorno = NULL;

	if ((SurfTemp = IMG_Load(File.data())) == NULL) {
		return NULL;
	}

	SurfRetorno = SDL_DisplayFormat(SurfTemp);
	SDL_FreeSurface(SurfTemp);

	(*GetSurfaces())[File] = SurfRetorno;

	return SurfRetorno;
}

bool CSurface::OnDraw(SDL_Surface* SurfDestino, SDL_Surface* SurfFonte, int X,
		int Y) {
	if (SurfDestino == NULL || SurfFonte == NULL) {
		return false;
	}

	SDL_Rect RectDestino;

	RectDestino.x = X;
	RectDestino.y = Y;

	SDL_BlitSurface(SurfFonte, NULL, SurfDestino, &RectDestino);

	return true;
}

bool CSurface::OnDraw(SDL_Surface* SurfDestino, SDL_Surface* SurfFonte, int X,
		int Y, int Xsrc, int Ysrc, int Wsrc, int Hsrc) {
	if (SurfDestino == NULL || SurfFonte == NULL) {
		return false;
	}

	SDL_Rect RectDestino;

	RectDestino.x = X;
	RectDestino.y = Y;

	SDL_Rect RectFonte;

	RectFonte.x = Xsrc;
	RectFonte.y = Ysrc;
	RectFonte.w = Wsrc;
	RectFonte.h = Hsrc;

	SDL_BlitSurface(SurfFonte, &RectFonte, SurfDestino, &RectDestino);

	return true;
}

bool CSurface::Transparent(SDL_Surface* SurfDest, int R, int G, int B) {
	if (SurfDest == NULL) {
		return false;
	}

	SDL_SetColorKey(SurfDest, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(
			SurfDest->format, R, G, B));

	return true;
}

std::map<std::string, SDL_Surface*>* CSurface::GetSurfaces() {
	static std::map<std::string, SDL_Surface*>* Surfaces =
			new std::map<std::string, SDL_Surface*>();
	return Surfaces;
}

